package uiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

/**
 * UI class that handles the IO of the Copy menu. Interacts with the DVDController in the controlLayer.
 * 
 * @author (Gruppe E) 
 * @version (06-11-2014)
 */
public class CopyUI
{
    private DVDController dvdCtr;

    /**
     * Constructor for objects of class CopyUI
     */
    public CopyUI(DVDController controller)
    {
        dvdCtr = controller;
    }

    /**
     * Starts the copyMenu UI.
     */
    public void start()
    {
        copyMenu();
    }

    /**
     * Runs a while-loop, supporting IO interactions with the user.
     */
    private void copyMenu()
    {
        boolean exit = false;

        while(!exit)
        {
            String choice = writeCopyMenu();
            if(choice.equals("1"))
            {
                addCopy();
            }

            else if(choice.equals("2"))
            {
                findCopy();
            }

            else if(choice.equals("3"))
            {
                updateCopy();
            }

            else if(choice.equals("4"))
            {
                deleteCopy();
            }

            else if(choice.equals("5"))
            {
                exit = true;
            }

            else
            {
                System.out.println("\nInvalid types. Please try again.");
            }
        }
    }

    /**
     * Displays a set of options and prompts an integer value. 
     */
    private String writeCopyMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        blankLines();
        System.out.println("*** CopyMenu ***");
        System.out.println("1. Add Copy");
        System.out.println("2. Find Copy");
        System.out.println("3. Update Copy");
        System.out.println("4. Delete Copy");
        System.out.println("5. Back");
        System.out.println("\nPlease choose a number");

        String choice = keyboard.nextLine();
        return choice;
    }

    /**
     * Requests the DVDController to add a copy to an existing DVD object matching the input value "barcode".
     * If no matching object is found a the copy with the specific serialnumber exists it prints 
     * a "failed" string.
     */
    private void addCopy()
    {
        blankLines();
        String barcode = inputBarcode();
        String serialNumber = inputSerialNumber();
        String purchaseDate = inputPurchaseDate();
        String price = inputPrice();
        if(validatePrice(price).equals("letters"))
        {
            System.out.println("\nPrice can't contain letters.");
        }
        else if(validatePrice(price).equals("priceOkay"))
        {
            int priceInt = Integer.parseInt(price);
            {
                String returnText = dvdCtr.createCopy(barcode, serialNumber, purchaseDate, priceInt);
                if(returnText.equals("invalidBarcode"))
                {            
                    System.out.println("\nNo DVD with the given barcode exists.");
                }
                else if(returnText.equals("copyExists"))
                {
                    System.out.println("\nCopy already exists.");
                }
                else if(returnText.equals("confirmed"))
                {
                    System.out.println("\nCopy succesfully created.");
                }
            }
        }
    }

    /**
     * Requests the DVDController to find a copy object. Writes the information about the copy to the UI
     * if it exists, else writes a "failed" message.
     */
    private void findCopy()
    {
        blankLines();
        String barcode = inputBarcode();
        String serialNumber = inputSerialNumber();
        Copy copy = dvdCtr.findCopy(barcode, serialNumber);
        if(copy != null)
        {
            System.out.println("\nCopy found \n");
            System.out.println("Purchase price: " + copy.getPurchasePrice());
            System.out.println("Purchase date: " + copy.getPurchaseDate());
        }

        else
        {
            System.out.println("\nCopy not found. Please try again");
        }

    }

    /**
     * Requests the DVDController to update a copy using input from the user.
     */
    private void updateCopy()
    {
        blankLines();
        System.out.println("\nPlease insert barcode and serial number of the copy you wish to update?");
        String barcode = inputBarcode();
        String serialNumber = inputSerialNumber();
        if(dvdCtr.findCopy(barcode, serialNumber) != null)
        {
            System.out.println("\nUpdate all of the information related to the copy");
            String price = inputPrice();
            if(validatePrice(price).equals("letters"))
            {
                System.out.println("\nPrice can't contain letters.");
            }
            else if(validatePrice(price).equals("priceOkay"))
            {
                int priceInt = Integer.parseInt(price);
                String newSerialNumber = inputSerialNumber();
                String purchaseDate = inputPurchaseDate();
                boolean state = inputState();
                dvdCtr.updateCopy(barcode, serialNumber, newSerialNumber, purchaseDate,  priceInt, state);             
            }
        }
        
        else
        {
            System.out.println("\nNo DVD with the barcode or a copy with the serial number exists.");
            System.out.println("Please try again");
        }
        
    }

    /**
     * Requests the DVDController to delete a DVD object.
     */
    private void deleteCopy()
    {
        blankLines();
        System.out.println("Enter the barcode and serialnumber of the copy you wish to delete");
        String barcode = inputBarcode();
        String serialNumber = inputSerialNumber();
        if(dvdCtr.deleteCopy(barcode, serialNumber))
        {
            System.out.println("Copy succesfully deleted");
        }
        else
        {
            System.out.println("Could not find copy to delete");
        }
    }

    /**
     * Prompts the user to enter a price integer.
     * @return  String
     */
    private String inputPrice()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the price of the copy: ");
        String price = scanner.nextLine();
        return price;
    }

    /**
     * Prompts the user to enter a barcode string.
     * @return  String
     */
    private String inputBarcode()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the barcode of the DVD the copy belongs to: ");
        String name = scanner.nextLine();
        return name;
    }

    /**
     * Prompts the user to enter a purachase-date string.
     * @return  String
     */
    private String inputPurchaseDate()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the purchase date of the copy: ");
        String date = scanner.nextLine();
        return date;
    }

    /**
     * Prompts the user to enter a serial number string.
     * @return  String
     */
    private String inputSerialNumber()
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\nEnter the Serialnumber of the copy: ");
        String num = scanner.nextLine();
        return num;
    }

    /**
     * Prompts the user to enter a boolean state.
     * @return  boolean
     */
    private boolean inputState()
    {
        blankLines();
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is the state of the copy\n");
        System.out.println("1. Available");
        System.out.println("2. Not Available");
        int input = scanner.nextInt();
        if(input == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * This method adds 5 blank lines.
     */
    private void blankLines()
    {
        for(int i = 0; i < 5; i++)
        {
            System.out.println();
        }
    }

    /**
     * Returns "letters" if the number contains letters.
     * Otherwise returns "priceOkay".
     *
     * @param price The price of the copy.
     * @return returnString A string that indicates wether or not the phone number contains letters.
     */
    private String validatePrice(String price)
    {
        String returnString = "priceOkay";
        try
        {
            int priceInt = Integer.parseInt(price);
        }
        catch(NumberFormatException e)
        {
            returnString = "letters";
        }

        return returnString;
    }
}
